package com.platform.usercenter.service.impl;


import com.alibaba.nacos.common.utils.CollectionUtils;
import com.platform.common.core.domain.model.LoginUser;
import com.platform.common.core.utils.DateUtils;
import com.platform.common.security.utils.SecurityUtils;
import com.platform.usercenter.domain.ContactFrequent;
import com.platform.usercenter.domain.ContactRelation;
import com.platform.usercenter.domain.vo.ContactVo;
import com.platform.usercenter.mapper.ContactFrequentMapper;
import com.platform.usercenter.mapper.ContactRelationMapper;
import com.platform.usercenter.service.IContactFrequentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 常用联系人通讯录Service业务层处理
 *
 * @author platform
 * @date 2023-10-08
 */
@Service
public class ContactFrequentServiceImpl implements IContactFrequentService {
	@Resource
	private ContactFrequentMapper contactFrequentMapper;

	@Resource
	private ContactRelationMapper contactRelationMapper;

	/**
	 * 查询常用联系人通讯录
	 *
	 * @param contactId 常用联系人通讯录主键
	 * @return 常用联系人通讯录
	 */
	@Override
	public ContactFrequent selectContactFrequentByContactId( Long contactId ) {
		return contactFrequentMapper.selectContactFrequentByContactId( contactId );
	}

	/**
	 * 查询常用联系人通讯录列表
	 *
	 * @param contactFrequent 常用联系人通讯录
	 * @return 常用联系人通讯录
	 */
	@Override
	public List< ContactFrequent > selectContactFrequentList( ContactFrequent contactFrequent ) {
		return contactFrequentMapper.selectContactFrequentList( contactFrequent );
	}

	/**
	 * 新增常用联系人通讯录
	 *
	 * @param contactFrequent 常用联系人通讯录
	 * @return 结果
	 */
	@Override
	public int insertContactFrequent( ContactFrequent contactFrequent ) {
		//先去查
		List< ContactFrequent > contactFrequents=contactFrequentMapper.selectFrequentByUserId( contactFrequent.getUserId() );
		if(CollectionUtils.isNotEmpty( contactFrequents )) {
			LoginUser loginUser=SecurityUtils.getLoginUser( );
			Long userId=loginUser.getSysUser( ).getUserId( );
			ContactRelation contactRelation=new ContactRelation( );
			contactRelation.setCreateTime( DateUtils.getNowDate( ) );
			contactRelation.setCreateBy( loginUser.getSysUser( ).getNickName() );
			contactRelation.setUserId( userId );
			contactRelation.setContactId( String.valueOf( contactFrequents.get( 0 ).getContactId( ) ) );
			//查关系表当前用户是否有此常用联系人了
			List< ContactRelation > contactRelations=contactRelationMapper.selectContactRelationList( contactRelation );
			if(CollectionUtils.isNotEmpty( contactRelations )){
				contactRelation.setIsDelete( "N" );
				return contactRelationMapper.updateContactRelation( contactRelation );
			}
			return contactRelationMapper.insertContactRelation( contactRelation );
		}
		contactFrequent.setCreateTime( DateUtils.getNowDate( ) );
		contactFrequentMapper.insertContactFrequent( contactFrequent );
		//常用联系人关系表
		LoginUser loginUser=SecurityUtils.getLoginUser( );
		Long userId=loginUser.getSysUser( ).getUserId( );
		ContactRelation contactRelation=new ContactRelation( );
		contactRelation.setCreateTime( DateUtils.getNowDate( ) );
		contactRelation.setCreateBy( loginUser.getSysUser( ).getNickName() );
		contactRelation.setUserId( userId );
		contactRelation.setContactId( String.valueOf( contactFrequent.getContactId( ) ) );
		return contactRelationMapper.insertContactRelation( contactRelation );

	}

	/**
	 * 修改常用联系人通讯录
	 *
	 * @param contactFrequent 常用联系人通讯录
	 * @return 结果
	 */
	@Override
	public int updateContactFrequent( ContactFrequent contactFrequent ) {
		contactFrequent.setUpdateTime( DateUtils.getNowDate( ) );
		return contactFrequentMapper.updateContactFrequent( contactFrequent );
	}

	/**
	 * 批量删除常用联系人通讯录
	 *
	 * @param userIds 需要删除的常用联系人通讯录主键
	 * @return 结果
	 */
	@Override
	public int deleteContactFrequentByContactIds( Long[] userIds ) {
		ContactVo contactVo=new ContactVo( );
		//更据用户id查询常用联系人表中的contactId
		ArrayList<Long> contactIds=contactFrequentMapper.selectContactIdByUserIds(userIds);
		contactVo.setContactIds( contactIds );

        Long userId=SecurityUtils.getLoginUser( ).getSysUser( ).getUserId( );
		contactVo.setRelationUserId( userId );
		return contactFrequentMapper.deleteContactFrequentByContactIds( contactVo );
	}

	/**
	 * 删除常用联系人通讯录信息
	 *
	 * @param contactId 常用联系人通讯录主键
	 * @return 结果
	 */
	@Override
	public int deleteContactFrequentByContactId( Long contactId ) {
		ContactVo contactVo=new ContactVo( );
		contactVo.setContactId( contactId );
//		Long userId=SecurityUtils.getLoginUser( ).getSysUser( ).getUserId( );
		contactVo.setRelationUserId( 1L );
		return contactFrequentMapper.deleteContactFrequentByContactId( contactVo );
	}

	@Override
	public List< ContactFrequent > selectContactFrequentListByContact( ContactVo contactVo ) {
		return contactFrequentMapper.selectContactFrequentListByContact( contactVo );
	}
}
